package fr.istic.linkedcv.client.presentation.clickhandlers;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

import fr.istic.linkedcv.client.presentation.Ui;
import fr.istic.linkedcv.client.services.GroupeService;
import fr.istic.linkedcv.client.services.GroupeServiceAsync;

public class JoinGroupeClickHandler implements ClickHandler {
	
	private final GroupeServiceAsync groupeServiceAsync = GWT.create(GroupeService.class);
	private String groupeName;
	private Ui ui;
	public JoinGroupeClickHandler(String groupeName, Ui ui) {
		this.groupeName = groupeName;
		this.ui = ui;
	}
	@Override
	public void onClick(ClickEvent event) {
		
		groupeServiceAsync.joinGroupe(groupeName, new AsyncCallback<Boolean>() {
			
			@Override
			public void onSuccess(Boolean result) {
				if(result){
					ui.addHistoryItem("groupe-"+groupeName);
				}else{
					ui.fireCurrentHistoryState();
				}
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});

	}

}
